package com.bnz.servlet;

import com.alibaba.fastjson.JSON;
import com.bnz.entity.Classes;
import com.bnz.entity.PageResult;
import com.bnz.entity.R;
import com.bnz.entity.Student;
import com.bnz.service.ClassesService;
import com.bnz.service.StudentService;
import com.bnz.service.impl.ClassesServiceImpl;
import com.bnz.service.impl.StudentServiceImpl;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/classes")
public class ClassesServlet extends HttpServlet {
    private ClassesService classesService;

    @Override
    public void init() throws ServletException {
        classesService = new ClassesServiceImpl();
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 0. 设置相应内容及字符集编码，将查询的内容送到前端页面上
        resp.setContentType("text/html;charset=utf8");
        // 1. 查询所有班级列表
        List<Classes> classes = classesService.findAll();
        // 2. 将classes对象转换为R对象
        R r = R.ok().put("list", classes);
        // 3. 输出
        resp.getWriter().println(JSON.toJSONString(r));
        // 4. 刷新
        resp.getWriter().flush();
        }
    }

